package com.groupfive.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfive.exception.CustomException;
import com.groupfive.mapper.AdminMapper;
import com.groupfive.pojo.Account;
import com.groupfive.pojo.Admin;
import com.groupfive.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public int add(Admin admin) {
        String username = admin.getUsername();
        Admin dbAdmin = adminMapper.selectByUsername(username);
        if (dbAdmin != null){
            throw new CustomException("500","账号已存在");
        }
        if (StrUtil.isBlank(admin.getPassword())){
            admin.setPassword("admin");
        }
        if (StrUtil.isBlank(admin.getName())){
            admin.setName(admin.getUsername());
        }
        admin.setRole("ADMIN");
        return adminMapper.insert(admin);
    }

    @Override
    public int update(Admin admin) {
        return adminMapper.update(admin);
    }

    @Override
    public int deleteById(Long id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids){
            adminMapper.deleteById(id);
        }
    }

    @Override
    public List<Admin> selectAll(Admin admin) {
        return adminMapper.selectAll(admin);
    }

    @Override
    public Admin selectById(Long id) {
        return adminMapper.selectById(id);
    }

    @Override
    public PageInfo<Admin> selectPage(Admin admin, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Admin> list = adminMapper.selectAll(admin);
        return PageInfo.of(list);
    }

    @Override
    public Admin login(Account account) {
        String username = account.getUsername();
        Admin dbAdmin = adminMapper.selectByUsername(username);
        if (dbAdmin == null){
            System.out.println("账号不存在");
            throw new CustomException("500","账号不存在");
        }
        String password = account.getPassword();
        if (!password.equals(dbAdmin.getPassword())){
            System.out.println("密码错误");
            throw new CustomException("500","账号或密码错误");
        }
        return dbAdmin;
    }

    @Override
    public void updatePassword(Account account) {
        Long id = account.getId();
        Admin admin = adminMapper.selectById(id);
        if (!admin.getPassword().equals(account.getPassword())){
            throw new CustomException("500","原密码错误");
        }else if (admin.getPassword().equals(account.getNewPassword())){
            throw new CustomException("500","新密码与原密码相同");
        }
        admin.setPassword(account.getNewPassword());
        this.update(admin);
    }
}
